package com.zff.web.outsideInter.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zff.web.constants.GeoPathPlanConstants;
import com.zff.web.outsideInter.GaoDeMapService;
import com.zff.wechat.util.HttpUtils;

@Service
public class GaoDeMapServiceImpl implements GaoDeMapService {
	
	private static final Log LOG = LogFactory.getLog(GaoDeMapServiceImpl.class);

	private static final String output = "JSON";
	
	private static final String webServiceKey = "dd5af31087a2d210c0b297015aa3dd12";
	
	private static final String webPlatformKey = "aa733be269ddcad9f068ed699f6f505d";
	
	/**
	 * <p>Title: getCoordinateByAddress</p>   
	 * <p>Description: 根据地址获取坐标值   
	 * @param address
	 * @return
	 * @throws Exception   
	 * @see com.zff.web.outsideInter.GaoDeMapService#getCoordinateByAddress(java.lang.String)
	 */
	@Override
	public String getCoordinateByAddress(String address) throws Exception {
		
		String url = "http://restapi.amap.com/v3/geocode/geo";
		
		Map<String, String> params = new HashMap<>();
		params.put("address", address);
		params.put("output", output);
		params.put("key", webServiceKey);
		
		String response = HttpUtils.sendPost(url, params);
		JSONObject jsonObject = JSONObject.parseObject(response);
		String count = jsonObject.getString("count");
		String location = null;
		if (Integer.parseInt(count) > 0) {
			JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
			JSONObject jsonArray_node = jsonArray.getJSONObject(0);
			location = jsonArray_node.get("location").toString();
		}else{
			LOG.info("无返回数据");
			throw new Exception("无返回数据");
		}
		
		return location;
	}

	/**
	 * <p>Title: pathPlanningCon</p>   
	 * <p>Description:  路径规划-返回地图url的中转方法
	 * @param guestRequest
	 * @return
	 * @throws Exception   
	 * @see com.zff.web.outsideInter.GaoDeMapService#pathPlanningCon(java.lang.String)
	 */
	@Override
	public String pathPlanningCon(String guestRequest) throws Exception {
		// 拆分地址为from 和 to
		Map<String, String> splitAddressMap = splitAddress(guestRequest);
		String from = splitAddressMap.get("from");
		String to = splitAddressMap.get("to");
		String from_Location_Coordinate = getCoordinateByAddress(from);
		String to_Location_Coordinate = getCoordinateByAddress(to);
		
		String pathResult = pathPlanning(from_Location_Coordinate,to_Location_Coordinate);
		return pathResult;
	}
	
	/**
	 * <p>Title: pathPlanningConByMap</p>   
	 * <p>Description: 总控制 传入用户发送的“位置从XX路到XX路”，然后解析出两个地址，分别获取两个地址的经纬度坐标，封装好传入封装返回地图url的方法中直接返回
	 * @param guestRequest
	 * @return
	 * @throws Exception   
	 * @see com.zff.web.outsideInter.GaoDeMapService#pathPlanningConByMap(java.lang.String)
	 */
	public String pathPlanningConByMap(String guestRequest) throws Exception {
		Map<String, String> splitAddressMap = splitAddress(guestRequest);
		String start = splitAddressMap.get("from");
		String dest = splitAddressMap.get("to");
		
		String start_Location_Coordinate = getCoordinateByAddress(start);
		String dest_Location_Coordinate = getCoordinateByAddress(dest);
		
		Map<String, String> map = new HashMap<>();
		map.put("start", start_Location_Coordinate);
		map.put("dest", dest_Location_Coordinate);
		map.put("destName", dest);
		map.put("naviBy", GeoPathPlanConstants.walk.getCode());
		
		String pathPlanningByMap = pathPlanningByMap(map);
		return pathPlanningByMap;
	}
	
	
	/**
	 * 
	 * @Title: pathPlanningByMap   
	 * @Description: 封装返回地图url数据
	 * @param: @param map
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws String start,String dest,String destName,String naviBy
	 */
	public String pathPlanningByMap(Map<String,String> map) throws Exception{
		String url = "https://m.amap.com/navi/";
		
		Map<String, String> params = new HashMap<>();
		params.put("start", map.get("start"));
		params.put("dest", map.get("dest"));
		params.put("destName", map.get("destName"));
		params.put("naviBy", map.get("naviBy"));
		params.put("key", webPlatformKey);
		
		String respUrl =url+"?start="+map.get("start")+"&dest="+map.get("dest")+"&destName="+map.get("destName")+"&naviBy="+map.get("naviBy")+"&key="+webPlatformKey;
		return "您需要的路径已经帮您规划好了，<a href='"+respUrl+"'>点击我哦！</a>";
	}
	
	/**
	 * @Title: pathPlanning   
	 * @Description: 路径规划-返回字符串规划的中转方法   
	 * @param: @param from
	 * @param: @param to
	 * @param: @return
	 * @param: @throws Exception      
	 * @return: String      
	 * @throws
	 */
	public String pathPlanning(String from,String to) throws Exception{
		
		String url ="http://restapi.amap.com/v3/direction/walking";
		Map<String, String> params = new HashMap<>();
		params.put("output", output);
		params.put("key", webServiceKey);
		params.put("origin", from); // 出发地
		params.put("destination", to); // 目的地
		
		String response = HttpUtils.sendPost(url, params);
		
		JSONObject jsonObject = JSONObject.parseObject(response);
		String count = jsonObject.getString("count");
		String total_step = null;
		if (Integer.parseInt(count) > 0) {
			String route = jsonObject.getString("route");
			JSONObject routeJsonObject = JSONObject.parseObject(route);
			JSONArray jsonArray = routeJsonObject.getJSONArray("paths");
			
			JSONObject jsonArray_node = jsonArray.getJSONObject(0);
			JSONArray stepsJsonArray = jsonArray_node.getJSONArray("steps");
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < stepsJsonArray.size(); i++) {
				String steps = stepsJsonArray.get(i).toString();
				JSONObject stepsJsonObject = JSONObject.parseObject(steps);
				String step = stepsJsonObject.getString("instruction");
				if (i<stepsJsonArray.size()-1) {
					sb.append(step+",");
				}else{
					sb.append(step);
				}
			}
			total_step = sb.toString();
		}
		return total_step;
	}
	

	/*
	 * 拆分地址为from 和 to
	 */
	public Map<String,String> splitAddress(String path){
		int from = path.indexOf("从");
		int to = path.indexOf("到");
		String firstSub = path.substring(from+1, to);
		String endSub = path.substring(to+1, path.length());
		Map<String,String> splitAddressMap = new HashMap<>();
		splitAddressMap.put("from", firstSub);
		splitAddressMap.put("to", endSub);
		return splitAddressMap;
	}

	/**
	 * <p>Title: userLocation</p>   
	 * <p>Description: 标注-定位  
	 * @param latitude
	 * @param longitude
	 * @return   
	 * @see com.zff.web.outsideInter.GaoDeMapService#userLocation(java.lang.String, java.lang.String)
	 */
	@Override
	public String userLocation(String latitude, String longitude) {
		 String url = "http://m.amap.com/navi/";
		 String dest = longitude+","+latitude;
		 String destName = "当前位置";
		 String respUrl = url+"?dest="+dest+"&destName="+destName+"&key="+webPlatformKey;
		 return "您的位置已经确定，<a href='"+respUrl+"'>点击我哦！</a>"+dest;
	}
	
}
